package com.bawei.park2211a.person


import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter

import com.bawei.base.mvvm.BaseActivity

import com.bawei.park2211a.adapter.DepartmentAdapter
import com.bawei.park2211a.adapter.PersonAdapter
import com.bawei.park2211a.databinding.ActivityPersonBinding
import com.blankj.utilcode.util.SPUtils
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/app/person/PersonActivity")
@AndroidEntryPoint
class PersonActivity : BaseActivity<ActivityPersonBinding, PersonViewModel>() {
    private lateinit var personAdapter: PersonAdapter

    private lateinit var departmentAdapter: DepartmentAdapter

    override fun initView() {
        viewModel.personList()
        personAdapter = PersonAdapter()
        departmentAdapter = DepartmentAdapter()
        binding.recPersonList.adapter = personAdapter
        binding.recDepartment.adapter = departmentAdapter
        binding.recPersonList.layoutManager = LinearLayoutManager(this)
        binding.recDepartment.layoutManager = GridLayoutManager(this, 2)
        viewModel.departmentPersonNumber(3)
        binding.addPerson.setOnClickListener {
            ARouter.getInstance().build("/app/person/AddPersonActivity").navigation()
        }
    }

    override fun initData() {
        viewModel.personListSuccess.observe(this) {
            personAdapter.data.clear()
            personAdapter.data.addAll(it)
            personAdapter.notifyDataSetChanged()
        }
        viewModel.departmentPersonNumberSuccess.observe(this) {
            departmentAdapter.data.clear()
            departmentAdapter.data.add(
                DepartmentPerson(
                    "研发部",
                    SPUtils.getInstance().getInt("RDNumber")
                )
            )
            departmentAdapter.data.add(DepartmentPerson("技术部", it.size))
            departmentAdapter.data.add(
                DepartmentPerson(
                    "市场部",
                    SPUtils.getInstance().getInt("MDNumber")
                )
            )
            departmentAdapter.notifyDataSetChanged()
        }


    }
}